package com.szwistar.common.meta;

import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.szwistar.common.comm.jsonrpc.JsonRpcMessageCall;
import com.szwistar.common.comm.jsonrpc.JsonRpcMessageReturn;
import com.szwistar.common.datastruct.JsonMapper;
import com.szwistar.common.meta.condition.*;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DemoObjectService {
    static final Logger LOG = LoggerFactory.getLogger(DemoObjectService.class);

    public static void main(String[] args) throws Exception {
        PropertyConfigurator.configure("log4j.pc.properties");
        ObjectRegistry.ObjectMetaInfo metaInfo = ObjectRegistry.register(DemoConfig.class);
        String json = JsonMapper.toJson(metaInfo);
        LOG.debug("{}", json);

        JdbcConnectionSource dbconn = new JdbcConnectionSource("jdbc:sqlite:./projects/ate-demo.db");
        ObjectService<DemoConfig> demoConfigSvc = ObjectService.createService(dbconn, "demoConfigSvc", DemoConfig.class, true);
        TableUtils.createTableIfNotExists(dbconn, DemoConfig.class);

        JsonRpcMessageCall request;
        JsonRpcMessageReturn ret;

        Map<String, Object> params = new HashMap<>();
        params.put("radioMode", "GSM");
        params.put("bandId", 123);
        params.put("dlFreq", 456);
        params.put("ulFreq", 567);
        request = new JsonRpcMessageCall(null, null, "demoConfigSvc.create", JsonMapper.toJson(params));
        ret = demoConfigSvc.create(request);
        int id1 = Integer.parseInt(ret.getBody());

        params.put("radioMode", "LTE");
        request = new JsonRpcMessageCall(null, null, "demoConfigSvc.create", JsonMapper.toJson(params));
        ret = demoConfigSvc.create(request);
        int id2 = Integer.parseInt(ret.getBody());

        params.clear();
        params.put("id", id1);
        params.put("radioMode", "GSM2");
        request = new JsonRpcMessageCall(null, null, "demoConfigSvc.update", JsonMapper.toJson(params));
        ret = demoConfigSvc.update(request);

        request = new JsonRpcMessageCall(null, null, "demoConfigSvc.getAll", null);
        ret = demoConfigSvc.getAll(request);

        request = new JsonRpcMessageCall(null, null, "demoConfigSvc.getById", Integer.toString(id2));
        ret = demoConfigSvc.getById(request);

        json = JsonMapper.toJson(new Integer[]{id1,id2});
        request = new JsonRpcMessageCall(null, null, "demoConfigSvc.getByIds", json);
        ret = demoConfigSvc.getByIds(request);

        List<Condition> conditions = new ArrayList<Condition>();
        conditions.add(new ConditionEqual<Integer>("bandId", 123));
        conditions.add(new ConditionLike("radioMode", "GSM%"));
        json = JsonMapper.toJson(conditions);
        LOG.debug("条件格式: {}", json);
        request = new JsonRpcMessageCall(null, null, "demoConfigSvc.get", json);
        ret = demoConfigSvc.get(request);

        conditions.clear();
        conditions.add(new ConditionGreatThan<Integer>("id", 10));
        request = new JsonRpcMessageCall(null, null, "demoConfigSvc.get", JsonMapper.toJson(conditions));
        ret = demoConfigSvc.get(request);

        conditions.clear();
        conditions.add(new ConditionBetween<Integer>("id", 41, 50));
        request = new JsonRpcMessageCall(null, null, "demoConfigSvc.get", JsonMapper.toJson(conditions));
        ret = demoConfigSvc.get(request);

        json = JsonMapper.toJson(new Integer[]{id1,id2});
        request = new JsonRpcMessageCall(null, null, "demoConfigSvc.delete", json);
        demoConfigSvc.delete(request);
    }
}
